package config

import (
	"github.com/hwcer/cosgo/random"
	"github.com/hwcer/yyds"
)

const (
	ChipAttrGroupProcessName    = "ChipAttrGroup"
	ChipSubAttrLimitProcessName = "ChipSubAttrLimit"
)

// 道具类型分析
func init() {
	yyds.Config.Register(&chip{})
}

func GetChipAttrGroupRandom(id int32) *random.Random {
	i := yyds.Config.Process.Get(ChipAttrGroupProcessName)
	if i == nil {
		return nil
	}
	g, ok := i.(map[int32]*random.Random)
	if !ok {
		return nil
	}
	return g[id]
}

func GetChipSubAttrLimitRandom(id int32) *random.Random {
	i := yyds.Config.Process.Get(ChipSubAttrLimitProcessName)
	if i == nil {
		return nil
	}
	g, ok := i.(map[int32]*random.Random)
	if !ok {
		return nil
	}
	return g[id]
}

type chip struct {
	HandleDefault
}

func (this *chip) Handle(c *yyds.CS, i any) {
	data := i.(*output)

	//主属性
	d := map[int32]*random.Random{}
	for k, v := range data.ChipAttr {
		r := &random.Random{}
		d[k] = r
		for j := 0; j < len(v.Attr); j++ {
			if v.Attr[j].Id > 0 && v.Attr[j].Weight > 0 {
				r.Add(int32(j), v.Attr[j].Weight)
			}
		}
	}
	c.Process.Set(ChipAttrGroupProcessName, d)
	//副属性条数
	subAttr := map[int32]*random.Random{}
	for k, v := range data.Chip {
		r := &random.Random{}
		subAttr[k] = r
		for j := 0; j < len(v.IniSubAttr); j++ {
			if v.IniSubAttr[j].Num > 0 && v.IniSubAttr[j].Weight > 0 {
				r.Add(v.IniSubAttr[j].Num, v.IniSubAttr[j].Weight)
			}
		}
	}
	c.Process.Set(ChipSubAttrLimitProcessName, subAttr)

	return
}
